<?php

// 服务器节点
class ServersController extends AdminController
{
    // 节点列表
    public function listAction()
    {
        $do = $this->get('do');
        switch ($do) {
            case 'list':
                $parameters['conditions'] = [];
                $parameters['order'] = 'ExpiredTime asc';
                $data = $this->_list(DunSeversModel::class, $parameters);
                foreach ($data['data'] as $item) {
                    $data_result[] = array(
                        'id' => $item->id,
                        'groupid' => $item->groupid,
                        'projectid' => $item->projectid,//项目编号
                        'project_name' => $item->project->name ?? '无',//项目编号
                        'account' => $item->group->title,
                        'InstanceId' => $item->InstanceId,
                        'InstanceChargeType' => $item->InstanceChargeType == 'PrePaid' ? '包年包月' : '按量计费',
                        'InternetChargeType' => $item->InternetChargeType == 'PayByBandwidth' ? '固定带宽' : '按量计费',
                        'ExpiredTime' => $item->ExpiredTime,
                        'HostName' => $item->HostName,
                        'Status' => $item->Status,
                        'CreationTime' => $item->CreationTime,
                        'SecurityGroupIds' => $item->SecurityGroupIds,
                        'PrivateIpAddress' => $item->PrivateIpAddress,
                        'InstanceName' => $item->InstanceName,
                        'OSName' => $item->OSName,
                        'PublicIpAddress' => $item->PublicIpAddress,
                        'InstanceType' => $item->InstanceType,
                        'RegionId' => $item->RegionId,
                    );
                }
                $this->ajaxresult(0, '', $data_result, array('count' => $data['count']));
                break;
        }
    }
    // 刷新

    // 更新镜像实例
    function refreshAction()
    {
        if ($this->ispost) {
            $groupid = $this->post('groupid');
            $regionid = $this->post('regionid');
            $account = DunGroupsModel::find($groupid);
            if ($account && $regionid) {
                $alibaba = new Aliyun($regionid, $account->accessKeyId, $account->accessSecret);
                $alibaba->setHost();
                $alidata = $alibaba->DescribeInstances();
                if ($alidata && $alidata['TotalCount'] > 0) {
                    foreach ($alidata['Instances']['Instance'] as $item) {
                        $ecs = DunSeversModel::where([
                            'InstanceId' => $item['InstanceId'],
                            'groupid' => $groupid
                        ])->find();
                        $data = [
                            'groupid' => $account->id,
                            'InstanceId' => $item['InstanceId'],
                            'InstanceChargeType' => $item['InstanceChargeType'],
                            'ExpiredTime' => $item['ExpiredTime'],
                            'HostName' => $item['HostName'],
                            'Status' => $item['Status'],
                            'CreationTime' => $item['CreationTime'],
                            'SecurityGroupIds' => $item['SecurityGroupIds'],
                            'PrivateIpAddress' => $item['VpcAttributes']['PrivateIpAddress']['IpAddress'][0] ?? '0.0.0.0',
                            'InternetChargeType' => $item['InternetChargeType'],
                            'InstanceName' => $item['InstanceName'],
                            'OSName' => $item['OSName'],
                            'PublicIpAddress' => $item['PublicIpAddress']['IpAddress'][0] ?? '0.0.0.0',
                            'InstanceType' => $item['InstanceType'],
                            'RegionId' => $item['RegionId'],
                            'AllConfig' => json_encode($item)
                        ];
                        if ($ecs) {
                            $ecs->save($data);
                        } else {
                            DunSeversModel::create($data);
                        }
                    }
                }
                $this->ajaxresult(0, '操作成功');
            }
            $this->ajaxresult(1, '账户不存在');
        }
        // 加载所有地区
        $this->view->assign('all_regionid', Aliyun::getConfig('ecs'));
        // 加载所有账户
        $this->view->assign('all_account', DunGroupsModel::select());
    }

    // 项目配置
    function configAction()
    {

    }

    // 新增编辑(手动新增节点)
    function saveAction()
    {
    }

    // aliyun 开节点
    function aliyunAction()
    {

    }

    // 项目流量
    function trafficAction()
    {

    }

    // 云节点关机
    function shutdownAction()
    {

    }

    // 云节点释放
    function flushAction()
    {

    }
}